﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameStudio.Common.BootStrappers;
using GameStudio.IDE.Common.Events;
using Microsoft.Practices.ServiceLocation;
using Utils.EventAggregator;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace GameStudio.IDE.Common.Bootstrappers
{
    class SaveXmlFileTask : IBootstrapper
    {
        public void Run()
        {
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<SaveXmlFileEvent>().Subscribe(OnSaveXmlFile);
        }

         private void OnSaveXmlFile(XmlTextReader XmlReader)
         {
             string [] PosotionValues = new string [3];
             string [] ColourValues =   new string [9];

             XmlReader.WhitespaceHandling = WhitespaceHandling.None;

             XmlDocument XmlDoc = new XmlDocument();

             XmlDoc.Load(XmlReader);

             while(XmlReader.Read())
             {
                 switch (XmlReader.NodeType)
                 {
                     case XmlNodeType.Element :
                         if(XmlReader.Name=="Voxel")
                         {
                             if(XmlReader.HasAttributes)
                             {
                                 for(int i = 0 ; i < XmlReader.AttributeCount ; i++)
                                 {
                                     XmlReader.MoveToAttribute(i);

                                     if(XmlReader.Name=="Position")
                                     {
                                       string Values = XmlReader.Value.ToString();

                                       for(int counter = 0 ; counter < 3 ; counter++)
                                       {
                                           PosotionValues[counter] = Values[counter].ToString();
                                       }

                                     }
                                     else if (XmlReader.Name=="Colour")
                                     {
                                         string Values = XmlReader.Value.ToString();

                                       for(int counter = 0 ; counter < 3 ; counter++)
                                       {
                                           ColourValues[counter] = Values[counter].ToString();
                                       }
                                     }

                                 }
                             }
                         }
                         break;

                     case XmlNodeType.EndElement :
                         break;

                 }
             }
         }
    }
}
